package com.bobi.tfca.ui.member

import android.app.AlertDialog
import android.os.Bundle
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentMemberRemoveBinding
import com.bobi.tfca.network.client.AppCache
import com.bobi.tfca.ui.adapter.GroupMemRmAdapter
import com.bobi.tfca.util.LogUtil

class MemberRemoveFragment: BaseFragment<FragmentMemberRemoveBinding, MemberRmFragmentVM>(
    FragmentMemberRemoveBinding:: inflate,
    MemberRmFragmentVM::class.java,
    true
) {
    // 会员RV
    private lateinit var memberRV: RecyclerView

    override fun initFragment(
        binding: FragmentMemberRemoveBinding,
        viewModel: MemberRmFragmentVM?,
        savedInstanceState: Bundle?
    ) {

        // 初始化标题
        val groupId = publicViewModel!!.chatRoom.value!!.groupId
        val groupName = publicViewModel!!.chatRoom.value!!.groupName

        // 初始化topPanel
        setTopPanelStyle(binding, groupName, viewModel!!)

        // 初始化RV
        initRecyclerView(binding)

        // 设置监听
        viewModel.apply {
            members.observe(viewLifecycleOwner){
                val adapter = memberRV.adapter as GroupMemRmAdapter
                adapter.memberResponseList = it
                adapter.notifyDataSetChanged()
            }
        }
    }


    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(
        binding: FragmentMemberRemoveBinding,
        groupName: String?,
        viewModel: MemberRmFragmentVM
    ) {
        val topPanel = binding.topPanel
        val leftImage = topPanel.leftImage
        leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        topPanel.rightButton.text = "移除"
        topPanel.rightButton.setOnClickListener {
            // 创建确认框
            val builder = AlertDialog.Builder(requireContext())
            builder.setTitle("移除")
            builder.setMessage("你确认移除群员吗")

            // 设置确认按钮和点击事件
            builder.setPositiveButton("确认") { dialog, which ->
                // 获得所有选择会员id
                val adapter = memberRV.adapter as GroupMemRmAdapter
                val memberIds = adapter.getCheckedMemberList()
                val memberId = AppCache.getMyInfo().memberId
                LogUtil.i("memberIds--------${memberIds.toString()}")
                if (memberId == null) {
                    LogUtil.e("未登录，AppCache为空")
                } else if (memberIds.size <= 0) {
                    Toast.makeText(requireActivity(), "未选择移除成员", Toast.LENGTH_SHORT).show()
                } else {
                    if (memberId in memberIds) {
                        Toast.makeText(requireActivity(), "不能移除自己", Toast.LENGTH_SHORT).show();
                    } else {
                        viewModel.removeGroupMember(publicViewModel!!, this, memberIds, adapter)
                    }
                }
            }

            // 设置取消按钮和点击事件
            builder.setNegativeButton("取消") { dialog, which ->
            }

            // 创建并显示确认框
            val dialog: AlertDialog = builder.create()
            dialog.show()
        }
        val title = topPanel.titleText
        // 群聊名称
        title.text = groupName
    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentMemberRemoveBinding
    ) {
        // 初始化RV
        memberRV = binding.memberRV
        // 设置布局排列方式，默认垂直排列
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        memberRV.layoutManager = linearLayoutManager
        // 添加分隔线
        memberRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        // 绑定adapter
        val memberAdapter = GroupMemRmAdapter(this, arrayListOf())
        memberRV.adapter = memberAdapter
    }

    override fun resumeFragment(viewModel: MemberRmFragmentVM?) {
        val groupId = publicViewModel!!.chatRoom.value!!.groupId
        super.resumeFragment(viewModel)
        viewModel!!.apply {
            findAllGroupMembers(publicViewModel!!, groupId, this@MemberRemoveFragment)
        }
    }
}